﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class BG_Manage_BG_Login : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            //Session["User"] = "";
            //Session["PWD"] = "";
            //Session["userID"] = "";
            Session.RemoveAll();
        }

    }

    protected void btnLogin_Click(object sender, ImageClickEventArgs e)
    {
        string loginname = txtUsername.Value.ToString();
        string loginpwd = txtPWD.Value.ToString();
        String checkCode = Request["checkCode"];
        string code = txtValidatecode.Value.ToString();
        code.ToUpper().ToString();

        if (loginname != "" && loginpwd != "")
        {
            if (checkCode == code.ToUpper().ToString())
            {
                ShowInfo(loginname, loginpwd);              
            }
            else
            {
                Page.ClientScript.RegisterStartupScript(Page.GetType(), "message", "<script language='javascript' defer>alert('验证码错误,请重新登录！');</script>");

                //Response.Write("<script lanuage=javascript>alert('验证码错误,请重新登录！');location='javascript:history.go(-1)'</script>");

            }
        }
        else
        {
            Page.ClientScript.RegisterStartupScript(Page.GetType(), "message", "<script language='javascript' defer>alert('用户名和密码不能为空，登录失败！');</script>");

        }
    }

    private void ShowInfo(string UserName, string PassWord)
    {
        VPINews.BLL.Admin bll_a = new VPINews.BLL.Admin();
        VPINews.Model.Admin model_a = bll_a.GetModel(UserName);

        if (model_a != null && model_a.PassWord == PassWord)
        {
            Session["User"] = UserName;
            Session["PWD"] = PassWord;
            Session["userID"] = model_a.ID;
            Page.Response.Redirect("BG_Index.htm");

        }
        else
        {

            Page.ClientScript.RegisterStartupScript(Page.GetType(), "message", "<script language='javascript' defer>alert('用户名或密码有误，登录失败！');</script>");

            //Response.Write("<script lanuage=javascript>alert('用户名或密码有误，登录失败！');location='javascript:history.go(-1)'</script>");
        }

    }


    protected void btnbtnReset_Click(object sender, ImageClickEventArgs e)
    {
        Session["User"] = "";
        Session["PWD"] = "";
        Session["userID"] = "";
        this.txtUsername.Value = null;
        this.txtPWD.Value = null;
        this.txtValidatecode.Value = null;
    }
}